package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.entity.Blacklist;
import com.example.speeddating.enums.ResponseCodeEnum;
import com.example.speeddating.exception.CommonException;
import com.example.speeddating.mapper.BlacklistMapper;
import com.example.speeddating.service.BlacklistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author tina
 * @since 2023-08-14 10:32:59
 */
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, Blacklist> implements BlacklistService {

    @Autowired
    private BlacklistMapper blacklistMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBlackList(Long id, Long black_user_id) {
        QueryWrapper<Blacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        queryWrapper.eq("black_user_id",black_user_id);
        Blacklist b = blacklistMapper.selectOne(queryWrapper);
        if (b!=null){
            throw new CommonException("已加入黑名单");
        }else {
            Blacklist blacklist = new Blacklist();
            blacklist.setUserId(id);
            blacklist.setBlackUserId(black_user_id);
            blacklistMapper.insert(blacklist);
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeBlackList(Integer id) {
        blacklistMapper.deleteById(id);
    }

    @Override
    public Page<Blacklist> getBlackList(Long id, int pageNum, int pageSize) {
        Page<Blacklist> page = new Page<>(pageNum,pageSize);
        return blacklistMapper.selectBlackList(page,id);
    }

    @Override
    public Blacklist selectBlackListByid(Long userId, Long blackId) {
        QueryWrapper<Blacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("black_user_id",blackId);
        return blacklistMapper.selectOne(queryWrapper);
    }
}
